<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="kafkaIntegrationConfigForm">
  <section formGroupName="clientConfiguration">
    @if (!isNew) {
      <ng-container *ngTemplateOutlet="topicFilters"></ng-container>
    }
    <section class="pb-6" tb-hint-tooltip-icon="{{ 'integration.send-only-payload-hint' | translate }}">
      <mat-slide-toggle formControlName="sendOnlyMsgPayload">
        <span translate>integration.send-only-payload</span>
      </mat-slide-toggle>
    </section>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.bootstrap-servers</mat-label>
      <input matInput formControlName="bootstrapServers" required>
      <tb-copy-button matSuffix
                      [class.!hidden]="!clientConfigurationFormGroup.get('bootstrapServers')?.value?.length"
                      [copyText]="clientConfigurationFormGroup.get('bootstrapServers').value">
      </tb-copy-button>
      <mat-icon matSuffix
                matTooltipClass="tb-error-tooltip"
                matTooltip="{{ 'integration.bootstrap-servers-required' | translate }}"
                [class.!hidden]="!clientConfigurationFormGroup.get('bootstrapServers').hasError('required')"
                class="tb-error">
        warning
      </mat-icon>
      <mat-icon matSuffix
                matTooltipClass="tb-error-tooltip"
                matTooltip="{{ 'integration.white-space-only' | translate }}"
                [class.!hidden]="!clientConfigurationFormGroup.get('bootstrapServers').hasError('onlyWhitespace')"
                class="tb-error">
        warning
      </mat-icon>
    </mat-form-field>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.topic</mat-label>
      <input matInput formControlName="topic">
      <tb-copy-button matSuffix
                      [class.!hidden]="!clientConfigurationFormGroup.get('topic')?.value?.length"
                      [copyText]="clientConfigurationFormGroup.get('topic').value">
      </tb-copy-button>
      <mat-icon matSuffix
                matTooltipClass="tb-error-tooltip"
                matTooltip="{{ 'integration.topic-required' | translate }}"
                [class.!hidden]="!clientConfigurationFormGroup.get('topic').hasError('required')"
                class="tb-error">
        warning
      </mat-icon>
    </mat-form-field>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>integration.key</mat-label>
      <input matInput formControlName="key">
      <mat-icon matSuffix matTooltip="{{ 'integration.key-hint' | translate }}">
        help
      </mat-icon>
    </mat-form-field>
    <mat-form-field class="mat-block mb-4" appearance="outline" subscriptSizing="dynamic">
      <mat-label translate>integration.client-id-prefix</mat-label>
      <input matInput formControlName="clientIdPrefix">
      <mat-icon matSuffix matTooltip="{{ 'integration.client-id-prefix-hint' | translate }}">
        help
      </mat-icon>
    </mat-form-field>
    <ng-container [ngTemplateOutlet]="executeRemotelyTemplate()">
    </ng-container>
    <mat-expansion-panel class="configuration-panel">
      <mat-expansion-panel-header>
        <mat-panel-description class="flex items-stretch justify-end" translate>
          integration.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.retries</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="retries">
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'integration.min-retries-message' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('retries').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.batch-size-bytes</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="batchSize">
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'integration.min-batch-size-bytes-message' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('batchSize').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.linger-ms</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="linger">
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'integration.min-linger-ms-message' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('linger').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.buffer-memory-bytes</mat-label>
          <input type="number" step="1" min="0" matInput formControlName="bufferMemory">
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'integration.min-buffer-memory-message' | translate }}"
                    [class.!hidden]="!clientConfigurationFormGroup.get('bufferMemory').hasError('min')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.acks</mat-label>
          <mat-select formControlName="acks" required>
            @for (ackValue of ackValues; track ackValue) {
              <mat-option [value]="ackValue">
                {{ ackValue }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.compression</mat-label>
          <mat-select formControlName="compression" required>
            @for (value of compressionValues; track value) {
              <mat-option [value]="value">
                {{ value }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        <div class="flex flex-col gap-3">
          <section class="security-setting mat-padding">
            <p translate>integration.other-properties</p>
            <tb-key-val-map
              class="security-setting-content"
              addText="integration.add-property"
              noDataText="integration.no-properties"
              formControlName="otherProperties">
            </tb-key-val-map>
          </section>
          <section class="security-setting mat-padding">
            <p translate>integration.kafka-headers</p>
            <div class="flex flex-col gap-6">
              <tb-key-val-map
                formControlName="kafkaHeaders"
                class="security-setting-content"
                noDataText="integration.no-headers-filter"
                addText="key-val.add-header"
                keyLabel="integration.header">
              </tb-key-val-map>
              <mat-form-field class="mat-block flex-1" appearance="outline">
                <mat-label translate>integration.charset-encoding</mat-label>
                <mat-select formControlName="kafkaHeadersCharset">
                  @for (charset of ToByteStandartCharsetTypesValues; track charset) {
                    <mat-option [value]="charset">
                      {{ ToByteStandartCharsetTypeTranslationMap.get(charset) | translate }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
            </div>
          </section>
          <ng-container [ngTemplateOutlet]="genericAdditionalInfoTemplate()">
          </ng-container>
        </div>
        <!--<mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.key-serializer</mat-label>
          <input required matInput formControlName="keySerializer">
          @if (clientConfigurationFormGroup.get('keySerializer').hasError('required')) {
            <mat-error>{{ 'integration.key-serializer-required' | translate }}</mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>integration.value-serializer</mat-label>
          <input required matInput formControlName="valueSerializer">
          @if (clientConfigurationFormGroup.get('valueSerializer').hasError('required')) {
            <mat-error>{{ 'integration.value-serializer-required' | translate }}</mat-error>
          }
        </mat-form-field>-->
      </ng-template>
    </mat-expansion-panel>
  </section>
  <ng-template #topicFilters>
    <section class="mb-6" [formGroup]="kafkaIntegrationConfigForm">
      <section class="security-setting mat-padding">
        <p translate>integration.topic-filters</p>
        <tb-integration-topic-filters [isEdit]="isEdit()" [integration]="integration()" formControlName="topicFilters">
        </tb-integration-topic-filters>
      </section>
    </section>
  </ng-template>
</section>
